package com.pubinfo.passbook.data.quartz.listenner;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pubinfo.passbook.data.config.ShiroConfig;
import com.pubinfo.passbook.data.quartz.utils.QuartzManager;
import com.pubinfo.passbook.common.entity.TAutoTask;
import com.pubinfo.passbook.common.mapper.TAutoTaskMapper;
import com.pubinfo.passbook.common.model.constant.TaskStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@Order(value = 1)
public class ScheduleJobInitListener implements CommandLineRunner {
  @Autowired
  QuartzManager quartzManager;
  @Autowired
  TAutoTaskMapper taskMapper;
  @Resource
  ShiroConfig config;
  
  @Override
  public void run(String... arg0) {
    try {
      List<TAutoTask> taskList = taskMapper.selectList(Wrappers.emptyWrapper());
      for (TAutoTask task : taskList) {
        //本地不起定时任务
        String profile = config.getActiveProfile();
        // && !"dev".equals(profile) && !"prolocal".equals(profile)
        if (TaskStatusEnum.RUNNING.getCode().equals(task.getStatus()) && !"dev1".equals(profile) && !"prolocal".equals(profile)) {
          quartzManager.addJob(task);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    
  }
  
}